package com.chinawayltd.ews.sample.taskDispatch;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chinawayltd.ews.sample.taskDispatch.pojo.BaseTask;
import com.chinawayltd.ews.sample.taskDispatch.pojo.TransportTaskCost;

import java.util.List;

/**
 * @program: greeProject.timingTaskCenter
 * @description:
 * @author: xiazj
 * @create: 2018-07-18 14:18
 **/
public class TaskGenerator {

    private static final String DELIMITER = "-";

    //站点信息
    private JSONObject location;
    //站点耗时
    private JSONObject timecost;
    //道路限行
    private JSONArray roadlimit;

    private TaskGenerator(Builder builder) {
        this.location = builder.location;
        this.timecost = builder.timecost;
        this.roadlimit = builder.roadlimit;
    }

    public JSONObject generaterTask(BaseTask baseTask){
        JSONObject vehicle = baseTask.getVehicle();
        TransportTaskCost transportOrder = this.getTransportOrder(baseTask.getOrderList(), vehicle);
        JSONObject result = null;

        return result;
    }

    private TransportTaskCost getTransportOrder(List<JSONObject> orderList, JSONObject vehicle){
        String cartype = vehicle.getString(TaskDispatchConstants.VEHICLE_VOLUME);
        TransportTaskCost transportOrder = new TransportTaskCost();

        for (int i = 0; i < orderList.size(); i++) {
            JSONObject order = orderList.get(i);
            String pickup = order.getString(TaskDispatchConstants.TASKLIST_PICKUP);
            String delivery = order.getString(TaskDispatchConstants.TASKLIST_DELIVERY);
            //装货站点信息
            JSONObject pickupObj = this.location.getJSONObject(pickup);
            //卸货站点信息
            JSONObject deliveryObj = this.location.getJSONObject(delivery);



        }

        return transportOrder;
    }

    public static class Builder {
        //站点信息
        private JSONObject location;
        //站点耗时
        private JSONObject timecost;
        //道路限行
        private JSONArray roadlimit;

        public TaskGenerator.Builder setLocation(JSONObject location) {
            this.location = location;
            return this;
        }

        public TaskGenerator.Builder setTimecost(JSONArray timecostArr) {
            JSONObject timecostMap = new JSONObject();
            for (int i = 0; i < timecostArr.size(); i++) {
                JSONObject timecostObj = timecostArr.getJSONObject(i);
                String loc1 = timecostObj.getString(TaskDispatchConstants.TIMECOST_LOC1);
                String loc2 = timecostObj.getString(TaskDispatchConstants.TIMECOST_LOC2);
                String cartype = timecostObj.getString(TaskDispatchConstants.TIMECOST_CARTYPE);
                Integer timecostNums = timecostObj.getInteger(TaskDispatchConstants.TIMECOST_TIMECOST);

                if(timecostNums != null && timecostNums > 0){
                    timecostMap.put(loc1 + DELIMITER + loc2 + DELIMITER + cartype, timecostNums);
                }
            }
            this.timecost = timecostMap;
            return this;
        }

        public TaskGenerator.Builder setRoadlimit(JSONArray roadlimit) {
            this.roadlimit = roadlimit;
            return this;
        }

        public TaskGenerator build(){ // 构建，返回一个新对象
            return new TaskGenerator(this);
        }
    }

}
